package com.gemom.gui;

import javax.swing.*;
import java.awt.BorderLayout;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.function.Function2D;
import org.jfree.data.function.NormalDistributionFunction2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.data.xy.XYSeries;

public class NormalDistributionItemsGUI extends JPanel{
 
    final ChartPanel chartPanel;
    final JFreeChart chart;
    private XYSeriesCollection dataset;
    XYSeries []series;
    private double startRange,endRange;////
    private String []nameSeries;
    
    public NormalDistributionItemsGUI(String []nameSeries){
        
       this.nameSeries=nameSeries;
       this.startRange=-10.0;
       this.endRange=40.0;
       series= new XYSeries[nameSeries.length];
       for(int i=0;i<nameSeries.length;i++) series[i] = new XYSeries(nameSeries[i]);
       dataset = new XYSeriesCollection();
       for(int i=0;i<this.series.length;i++)dataset.addSeries(series[i]);
        
       chart = ChartFactory.createXYLineChart(
            "",  "message per minute",  "f(x)",  dataset,  PlotOrientation.VERTICAL,
            false,  true,  false  );
     
       chartPanel = new ChartPanel(chart);
       chartPanel.setPreferredSize(new java.awt.Dimension(320, 230));
       this.setLayout(new BorderLayout());
       this.add(chartPanel);
    
    
    
    }
    
    public NormalDistributionItemsGUI(int numberitems){
       
       this.nameSeries=new String[numberitems];
       for(int i=0;i<numberitems;i++)this.nameSeries[i]="Item"+i;
       this.startRange=0.0;
       this.endRange=40.0;
        
       series= new XYSeries[numberitems];
       for(int i=0;i<numberitems;i++) series[i] = new XYSeries("item"+i);
       dataset = new XYSeriesCollection();
       for(int i=0;i<this.series.length;i++)dataset.addSeries(series[i]);
        
       chart = ChartFactory.createXYLineChart(
            "Normal Distribution",  "bps",  "f(x)",  dataset,  PlotOrientation.VERTICAL,
            true,  true,  false  );
     
       chartPanel = new ChartPanel(chart);
       chartPanel.setPreferredSize(new java.awt.Dimension(600, 300));
       this.setLayout(new BorderLayout());
       this.add(chartPanel);
       
    }//end constructor
    
    public void createChartSeries(double[][]meanAndStdDev){
           
        dataset.removeAllSeries(); 
          
        for(int i=0;i<meanAndStdDev[0].length;i++)
        {
           Function2D normal = new NormalDistributionFunction2D(meanAndStdDev[0][i], meanAndStdDev[1][i]);
           series[i] = DatasetUtilities.sampleFunction2DToSeries(normal, startRange, endRange, 250,nameSeries[i] );
           dataset.addSeries(series[i]);                                        //
        }
     }

    public void clearXYSeries(){  for(int i=0;i<series.length;i++)series[i].clear();}      
    
}
